package main

import (
	"fmt"
	"github.com/shirou/gopsutil/cpu"
	"github.com/shirou/gopsutil/disk"
	"github.com/shirou/gopsutil/load"
	"github.com/sirupsen/logrus"
	"time"
)

func main() {
	//getCPUInfo()
	getCPULoad()
	getDiskInfo()
}

func getCPUInfo() {
	cpuinfos, err := cpu.Info()
	if err != nil {
		logrus.Fatalln(err)
	}
	for _, ci := range cpuinfos {
		fmt.Println(ci)
	}
	//cpu使用率
	for {
		percent, _ := cpu.Percent(time.Second, false)
		fmt.Printf("cpu percent: %.4f%%\n", percent)
	}
}

func getCPULoad() {
	stat, err := load.Avg()
	if err != nil {
		logrus.Fatalln(err)
	}

	logrus.Info(stat)
}

func getDiskInfo() {
	partitions, err := disk.Partitions(true)
	if err != nil {
		logrus.Fatalln(err)
	}
	for _, partition := range partitions {
		fmt.Println("partition:", partition.String())
		diskInfo, _ := disk.Usage(partition.Mountpoint)
		fmt.Printf("dis info used:%v free:%v\n", diskInfo.Used, diskInfo.Free)
	}

	counters, err := disk.IOCounters()
	if err != nil {
		logrus.Fatalln(err)
	}
	for k, v := range counters {
		fmt.Println(k, v)
	}
}
